package com.qiantu.phone.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.hjq.umeng.UmengClient;
import com.qiantu.phone.R;
import com.qiantu.phone.aop.Log;
import com.qiantu.phone.aop.SingleClick;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.app.AppConfig;
import com.qiantu.phone.other.KeyboardWatcher;
import com.qiantu.phone.other.LimitCharLengthTextWatcher;
import com.qiantu.phone.utils.StringUtil;

/**
 * time   : 2021/12/22
 */
public final class PhoneAppealActivity extends AppActivity
        implements KeyboardWatcher.SoftKeyboardStateListener,
        TextView.OnEditorActionListener {

    @Log
    public static void start(Context context) {
        Intent intent = new Intent(context, PhoneAppealActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    private EditText editOldPhone;
    private EditText editDescription;
    private EditText editAccountInfo;
    private TextView mOldPhoneErrorHint;
    private View btnNext;

    @Override
    protected int getLayoutId() {
        return R.layout.replace_phone_activity;
    }

    @Override
    protected void initView() {
        UmengClient.init(AppApplication.getInstance(), AppConfig.isLogEnable());
        btnNext = findViewById(R.id.btn_next);
        editOldPhone = findViewById(R.id.edit_old_phone);
        editDescription = findViewById(R.id.edit_description);
        editAccountInfo = findViewById(R.id.edit_account_info);
        mOldPhoneErrorHint = findViewById(R.id.tv_old_phone_error_hint);
        setOnClickListener(btnNext);
        editOldPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.length() == 11){
                    btnNext.setEnabled(true);
                }else{
                    btnNext.setEnabled(false);
                }
            }
        });
        editOldPhone.setText(AppApplication.getInstance().getUserPhone());
        editDescription.addTextChangedListener(new LimitCharLengthTextWatcher(editDescription, 1000));
        editAccountInfo.addTextChangedListener(new LimitCharLengthTextWatcher(editAccountInfo, 1000));
    }

    @Override
    protected void initData() {
        postDelayed(() -> {
            KeyboardWatcher.with(PhoneAppealActivity.this)
                    .setListener(PhoneAppealActivity.this);
        }, 500);
    }

    @SingleClick
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_next:
                String oldPhone = editOldPhone.getText() == null ? null : editOldPhone.getText().toString().trim();
                if (!StringUtil.isPhoneLegal(oldPhone)) {
                    editOldPhone.setSelected(true);
                    mOldPhoneErrorHint.setText(R.string.phone_error_hint);
                    return;
                }
                mOldPhoneErrorHint.setText(null);
                editOldPhone.setSelected(false);
                String description = editDescription.getText() == null ? null : editDescription.getText().toString().trim();
                if(TextUtils.isEmpty(description)){
                    toast(R.string.please_enter_unavailable_reason);
                    return;
                }
                String accountInfo = editAccountInfo.getText() == null ? null : editAccountInfo.getText().toString().trim();
                if(TextUtils.isEmpty(accountInfo)){
                    toast(R.string.please_enter_old_account_situation);
                    return;
                }
                EnterNewPhoneActivity.start(this, oldPhone, description, accountInfo, 1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 友盟回调
        UmengClient.onActivityResult(this, requestCode, resultCode, data);
    }

    /**
     * {@link KeyboardWatcher.SoftKeyboardStateListener}
     */

    @Override
    public void onSoftKeyboardOpened(int keyboardHeight) {
        // 执行位移动画
    }

    @Override
    public void onSoftKeyboardClosed() {
        // 执行位移动画
    }

    /**
     * {@link TextView.OnEditorActionListener}
     */
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE && editOldPhone.isEnabled()) {
            // 模拟点击登录按钮
//            onClick(mCommitView);
            return true;
        }
        return false;
    }
}